<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

    // 应用公共文件
    use extra\Constant;
    
    /**
     * 生成请求参数的数字签名
     *
     * @param array $param
     * @return string
     */
    function gen_sign(array $param, $secret_key) {
        //0. 删除原数据中自带的sign值,防止干扰计算结果
        unset($param['sign']);
    
        //1. 按key由a到z排序
        ksort($param);
    
        foreach ($param as $key => $value) {
            if (is_array($value)) {
                $param[$key] = gen_sign($value, $secret_key);
            }
        }
    
        //2. 生成以&符链接的key=value形式的字符串
        $paramString = urldecode(http_build_query($param));
    
        //3. 拼接我们的服务秘钥，并md5加密
        return md5($paramString . $secret_key);
    
    }


    /**
     * 判断是否为手机号
     * @param string $mobile
     * @return int
     */
    function is_mobile($mobile) {
        $pattern = '/^1[345678]\d{9}$/'; //手机号
        return preg_match($pattern, $mobile);
    }
    
    
    //请求参数校验
    function paramCheck($request,$must_param){
        
        if(!is_array($request) || !is_array($must_param)){
            $data = array('msg'=>'参数不正确','code'=>101,'data'=>array());
            $this->ajaxReturn($data);
        }
        $must_array = [];
        $request_array = [];
        foreach($must_param as $key=>$val){
            $must_array[$key] = $key;
        }
        foreach($request as $key=>$val){
            $request_array[$key] = $key;
        }
        $diff_key = array_diff_key($must_array,$request_array);
        $msg_key = '';
        foreach($diff_key as $key=>$val){
            $msg_key .= $key.",";
        }
        if($msg_key){
            $msg = substr($msg_key,0,strrpos($msg_key,','));
            $data = ['msg'=>"参数错误[".$msg ."]必须存在",'code'=>Constant::ERROR,'data'=>[]];
            json($data)->send();
        }
        return true;
    }
    
    
    /**
     *   生成某个范围内的随机时间
     * @param <type> $begintime  起始时间 格式为 Y-m-d H:i:s
     * @param <type> $endtime    结束时间 格式为 Y-m-d H:i:s
     */
    function randomTimeByMonth($month) {
        $timestamp = strtotime( $month );
        $start_time = date( 'Y-m-1 00:00:00', $timestamp );
        $mdays = date( 't', $timestamp );
        $end_time = date( 'Y-m-' . $mdays . ' 23:59:59', $timestamp );
        $timestamp = mt_rand($start_time,$end_time);
        return $timestamp;
    }
    
    /*
     * 获取指定的月份时间戳
     */
    function getTheMonth($cha=1,$month='',$type=2)
    {
        //得到系统的年月
        $tmp_date=$month ? $month : date("Ym");
        //切割出年份
        $tmp_year=substr($tmp_date,0,4);
        //切割出月份
        $tmp_mon =substr($tmp_date,4,2);
        //相加
        if($type==1){
            $tmp_forwardmonth=mktime(0,0,0,$tmp_mon+$cha,1,$tmp_year);
        }
        //相减
        if($type==2){
            $tmp_forwardmonth=mktime(0,0,0,$tmp_mon-$cha,1,$tmp_year);
        }
        //        return $fm_forward_month=strtotime(date("Ym",$tmp_forwardmonth));
        return $tmp_forwardmonth;
        
    }
    
    /**
     * 根据用户id获取用户数据表
     */
    function getCustomerTableByCustomerID($customerID)
    {
        return "customer_".sprintf('%02d',($customerID % 100));
    }